import { createRouter, createWebHashHistory } from 'vue-router'
import { routerlist } from './baseRouter'
import { getToken } from '@/utils/auth'
import { useKeepAliveStore } from '@/stores/keep-alive'

const whitePathList = ['/invite'] // 白名单
const router = createRouter({
  mode: 'hash',
  history: createWebHashHistory('/web'),
  routes: routerlist,
  scrollBehavior: () => ({ left: 0, top: 0 })
})
router.beforeEach(async (to, from, next) => {
  if (whitePathList.includes(to.path)) {
    next()
    return
  }
  next()
})

router.afterEach((to, from) => {
  // keepAlive 表示该页面要缓存，keepAliveDeep 表示该页面是缓存页面的下级页面且该页面也需要缓存
  // subAlive 表示是由缓存的页面跳转过来的页面的：不用清空缓存页面列表，否则缓存的页面将失效,并且当前页面无需缓存
  const { title, keepAlive, name, keepAliveDeep, subAlive } = to.meta
  document.title = title
  const keepAliveStore = useKeepAliveStore()
  if (keepAlive && name) {
    if (keepAliveDeep) {
      keepAliveStore.add(name)
    } else {
      keepAliveStore.replace(name)
    }
  } else {
    if (!subAlive) {
      keepAliveStore.clear()
    }
  }
})
export default router
